package com.wh.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wh.service.IExt.ICustomFieldService;
import com.legend.utils.*;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.wh.beans.model.FieldModel;
import com.wh.dao.BaseDao;
import com.wh.dao.ext.CustomFieldDao;
import com.wh.dao.ext.CustomFormDao;
import com.wh.model.BaseModel;
import com.wh.model.CustomField;
import com.wh.model.CustomForm;

/*
 *author:徐洪昌
 *blog:http://cnblogs.com/xusir
 *datetime:2014-03-27 23:25:41
 */
public class CustomFieldService  extends BaseServiceImp implements ICustomFieldService{

	Logger log = Logger.getRootLogger();
    private CustomFieldService() {
    }

    //volatile修饰的写变量不能和之前的读写代码调整，读变量不能和之后的读写代码调整
    private volatile static ICustomFieldService instance = null;

    /**
     * 单例
     *
     * @return
     */
    public static ICustomFieldService getInstance() {
        if (instance == null) {
            synchronized (ICustomFieldService.class) {
                if (instance == null) {
                    instance = new CustomFieldService();
                }
            }
        }
        return instance;
    }
	/**
	 * 批量删除数据
	 */
	public ReturnResult delByIds(String ids) {
		SqlSession session = getSqlSession();
		ReturnResult result = new ReturnResult();
		try {
			CustomFieldDao dao = session.getMapper(CustomFieldDao.class);
			result = delByMap(dao, ids);
		} catch (Exception ex) {
			log.error(ex.getMessage());
		} finally {
			session.close();
		}
		return result;
	}

	/**
	 * 分页数据查询
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @param map
	 * @return
	 */
	public ReturnResult selectByMap(int pageIndex, int pageSize, Map<String, Object> map) {
		SqlSession session = getSqlSession();
		CustomFieldDao dao = session.getMapper(CustomFieldDao.class);
		int recordNum = (pageIndex - 1) * pageSize;
		List<CustomField> lst = session.selectList(ConfigUtils.getInstance().getDaoPackage(".CustomFieldDao.selectByMap"), map, new RowBounds(recordNum, pageSize));
		int total = dao.count(map);
		ReturnResult result = new ReturnResult();
		result.setMsg(i18nUtils.getVal("100001000"));
                result.setRows(lst);
		result.setTotal(total);
		result.setStatus(Status.SUCCESS);
		session.close();
		return result;
	}

	@Override
	public ReturnResult insertByModel(BaseModel model) {
		SqlSession session = getSqlSession();
		CustomFieldDao dao = session.getMapper(CustomFieldDao.class);
		FieldModel fm = new FieldModel();
		CustomField cf = (CustomField) model;
		String guid = cf.getFormGuid();
		model.setId(guid);
		CustomForm form = new CustomForm();
		form.setGuid(guid);
		CustomFormDao formDao = session.getMapper(CustomFormDao.class);
		List<CustomForm> forms = formDao.selectByModel(form);
		String tableName = "";
		ReturnResult result = new ReturnResult();
		if (forms.size() == 1) {
			tableName = forms.get(0).getDbtableName();
		} else {
			return result;
		}
		Integer inputProperty = cf.getInputProperty();
		Boolean notEmpty = inputProperty == null ? false : ((inputProperty & 8) == 8);
		fm.setAllowEmpty(notEmpty ? " NOT NULL" : "NULL");
		fm.setComment(cf.getFieldDescription());
		String dataType = StringUtils.getDataType(cf.getFieldType(), cf.getFieldSize());
		fm.setDataType(dataType);
		fm.setDefaultValue(cf.getDefaultValue());
		fm.setFieldName(cf.getFieldDbName());
		fm.setTableName(tableName);
		fm.setUnique(inputProperty == null ? false : (inputProperty & 4) == 4);
		dao.addField(fm);
		result = insertByModel(dao, model);
		session.close();
		return result;
	}

	@Override
	public ReturnResult editByModel(BaseModel model) {
		SqlSession session = getSqlSession();
		CustomFieldDao dao = session.getMapper(CustomFieldDao.class);
		ReturnResult result = editByModel(dao, model);
		session.close();
		return result;
	}

	/**
	 * 根据ID获取一条数据
	 * 
	 * @param id
	 * @return
	 */
	public ReturnResult selectById(String id) {
		SqlSession session = getSqlSession();
		ReturnResult result = new ReturnResult();
		result.setStatus(Status.ERROR);
		BaseDao dao = session.getMapper(CustomFieldDao.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<CustomField> lst = dao.selectByMap(map);
		if (lst.size() == 1) {
			result.setData(lst.get(0));
			result.setStatus(Status.SUCCESS);
		} else {
			result.setMsg(i18nUtils.getVal("100004000"));
        }
		session.close();
		return result;
	}

	/**
	 * 批量审核数据
	 * 
	 */
	public ReturnResult audit(int state, String[] idArr) {
		SqlSession session = getSqlSession();
		CustomFieldDao dao = session.getMapper(CustomFieldDao.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("idArr", idArr);
		map.put("state", state);
		Integer num = dao.audit(map);
		ReturnResult result = new ReturnResult();
		if (num > 0) {
			result.setMsg(i18nUtils.getVal("100001000"));
                    result.setStatus(Status.SUCCESS);
		}
		session.close();
		return result;
	}

	/**
	 * 排序
	 * 
	 * @param sorts
	 * @param ids
	 * @return
	 */
	public ReturnResult sort(String sorts, String ids) {
		ReturnResult result;
		SqlSession session = getSqlSession();
		CustomFieldDao dao = session.getMapper(CustomFieldDao.class);
		result = sort(session, dao, sorts, ids);
		if (session != null)
			session.close();
		return result;
	}
}
